package com.hiss.basic.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.hiss.basic.client.system.ISysDeptService;
import com.hiss.basic.model.system.request.SysDeptCreateRequest;
import com.hiss.basic.model.system.request.SysDeptQueryRequest;
import com.hiss.basic.model.system.request.SysDeptUpdateRequest;
import com.hiss.basic.model.system.response.SysDeptQueryResponse;
import com.hiss.basic.starter.core.model.R;
import com.hiss.basic.starter.core.utils.tree.TreeNode;
import com.hiss.basic.starter.log.annotation.Log;
import com.hiss.basic.starter.log.enums.BusinessTypeEnum;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.util.List;

/**
 * 系统部门 controller
 */
@RestController
@RequestMapping("/sys/dept")
public class SysDeptController {

    @Resource
    private ISysDeptService sysDeptService;

    @SaCheckPermission("sys:dept:list")
    @RequestMapping(value = "/list", method = RequestMethod.GET)
    public R<List<SysDeptQueryResponse>> list(SysDeptQueryRequest request) {
        List<SysDeptQueryResponse> deptList = sysDeptService.findList(request);
        return R.success(deptList);
    }

    @SaCheckPermission("sys:dept:list-tree")
    @RequestMapping(value = "/list/tree", method = RequestMethod.GET)
    public R<List<TreeNode<Long, Integer>>> listTree(SysDeptQueryRequest request) {
        List<TreeNode<Long, Integer>> deptList = sysDeptService.findTreeList(request);
        return R.success(deptList);
    }

    @SaCheckPermission("sys:dept:create")
    @Log(title = "新增部门", businessType = BusinessTypeEnum.INSERT)
    @RequestMapping(value = "create", method = RequestMethod.POST)
    public R<Void> create(@RequestBody SysDeptCreateRequest request) {
        sysDeptService.create(request);
        return R.success();
    }

    @SaCheckPermission("sys:dept:update")
    @Log(title = "更新部门", businessType = BusinessTypeEnum.UPDATE)
    @RequestMapping(value = "update", method = RequestMethod.PUT)
    public R<Void> update(@RequestBody SysDeptUpdateRequest request) {
        sysDeptService.update(request);
        return R.success();
    }

    @SaCheckPermission("sys:dept:remove")
    @Log(title = "删除部门", businessType = BusinessTypeEnum.DELETE)
    @RequestMapping(value = "remove", method = RequestMethod.DELETE)
    public R<Void> remove(Long id) {
        sysDeptService.remove(id);
        return R.success();
    }
}
